﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Collection.Interfaces;
using Collection.Services;
using Top.Api.Request;
using Top.Api.Response;
using Top.Api.Domain;
using Collection.Entities;
using Collection.Core;
using System.Threading;
using Collection.Utilities;
namespace Collection.Core
{
    /// <summary>
    /// 类目信息采集器
    /// </summary>
    public class CatalogCollector : CollectorBase
    {
        public readonly static CatalogCollector Instance;

        /// <summary>
        /// 默认构造函数（注册事件）
        /// </summary>
        private CatalogCollector()
        {
        }

        /// <summary>
        /// 静态初始化
        /// </summary>
        static CatalogCollector()
        {
            Instance = new CatalogCollector();
        }

        #region 采集类目根属性信息

        /// <summary>
        /// 依据类目Id采集类目根属性信息
        /// </summary>
        /// <param name="catalogIds">类目Id</param>
        public void GatherProperty(Int64 catalogId)
        {
            ItempropsGetRequest req = new ItempropsGetRequest();
            req.Fields = "pid,name";
            req.Cid = catalogId;
            ItempropsGetResponse response = CollectorBase.TaobaoApi.Execute(req);
            foreach (var itemProperty in response.ItemProps)
            {
                var property = new Property
                    {
                        CatalogId = catalogId,
                        Id = itemProperty.Pid,
                        Name = itemProperty.Name
                    };
                if (CollectionServiceFactory.PropertyService.HasProperty(catalogId, property.Id))
                {
                    CollectionServiceFactory.PropertyService.Update(property);
                }
                else
                {
                    CollectionServiceFactory.PropertyService.Insert(property);
                }
            }
        }

        #endregion

        #region PropertyValues相关

        /// <summary>
        /// 采集属性值数据
        /// </summary>
        /// <param name="catalogId">类目Id</param>
        /// <param name="propertyId">属性Id</param>
        public void GatherPropertyValues(Int64 catalogId, Int64 propertyId)
        {
            ItempropsGetRequest request = new ItempropsGetRequest();
            request.Fields = "pid,name,prop_values";
            request.Cid = catalogId;
            request.Pid = propertyId;
            Thread.Sleep(1000);
            ItempropsGetResponse response = CollectorBase.TaobaoApi.Execute(request);
            foreach (var property in response.ItemProps)
            {
                foreach (var value in property.PropValues)
                {
                    var serialNumber = Utility.GetGuid();
                    this.SavePropertyValue(this.ConvertToPropertyValue(catalogId, property, value, serialNumber, string.Empty));
                    if (value.IsParent)
                    {
                        this.GatherPropertyValues(request, String.Format(CollectorBase.PathFormat, property.Pid, value.Vid), serialNumber);
                    }
                }
            }
        }

        /// <summary>
        /// 递归获取属性值的字属性信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="path">属性值对路径</param>
        /// <param name="parent"></param>
        private void GatherPropertyValues(ItempropsGetRequest request, String path, string parent)
        {
            request.Pid = null;
            request.ChildPath = path;
            Thread.Sleep(1000);
            ItempropsGetResponse response = CollectorBase.TaobaoApi.Execute(request);
            foreach (var property in response.ItemProps)
            {
                foreach (var value in property.PropValues)
                {
                    var serialNumber = Utility.GetGuid();
                    this.SavePropertyValue(this.ConvertToPropertyValue((long)request.Cid, property, value, serialNumber, parent));
                    if (value.IsParent)
                    {
                        this.GatherPropertyValues(request, String.Concat(path, CollectorBase.CreatePath(property.Pid, value.Vid)), serialNumber);
                    }
                }
            }
        }

        /// <summary>
        /// 创建一个本地的属性值对象
        /// </summary>
        /// <param name="itemProperty"></param>
        /// <param name="itemPropertyValue"></param>
        /// <param name="id"></param>
        /// <param name="parent"></param>
        /// <returns></returns>
        private PropertyValue ConvertToPropertyValue(Int64 catalogId, ItemProp itemProperty, PropValue itemPropertyValue, string serialNumber, string parent)
        {
            return new PropertyValue
            {
                Id = itemPropertyValue.Vid,
                Name = itemPropertyValue.Name,
                PropertyName = itemProperty.Name,
                PropertyId = itemProperty.Pid,
                SerialNumber = serialNumber,
                Parent = parent,
                CatalogId = catalogId,
                Alias = itemPropertyValue.NameAlias
            };
        }

        /// <summary>
        /// 保存属性值信息
        /// </summary>
        private void SavePropertyValue(PropertyValue propertyValue)
        {
            if (CollectionServiceFactory.PropertyValueService.HasPropertyValue(propertyValue))
            {
                CollectionServiceFactory.PropertyValueService.Update(propertyValue);
            }
            else
            {
                CollectionServiceFactory.PropertyValueService.Insert(propertyValue);
            }
        }
        #endregion
    }
}
